﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TWS.Controller.Paging;
using System.IO;
using TWS.Client;
using System.Data;

public partial class admin_QuanLySanPham : BaseAdminPage
{
    public int id;
    public string loaisanpham;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            id = Convert.ToInt32(Request.QueryString["id"]);
            loaisanpham = Request.QueryString["type"];
            //MasterPageDetail m = this.Master as MasterPageDetail;
            //m.loaisanphamid = id;
            LoadSanPham();

            //tab them moi
            if (id!=0)
            {
                LoadData();
            }
            else
            {
                pnTab2.Visible = false;
            }
            
        }
    }

    private void LoadSanPham()
    {
        string tmpPage = Request.QueryString["pg"];
        int currentPage = 1;
        if (tmpPage != null && tmpPage != "")
        {
            currentPage = Convert.ToInt32(tmpPage);
        }
        SanPhamDataSet spDs = new SanPhamHelper().LoadSanPhamTheoLoai(currentPage, PagingSize, "NgayDang", "DESC", id);

        repSanPham.DataSource = spDs.SanPham;
        repSanPham.DataBind();

        TotalRecord = new SanPhamHelper().TongSanPhamTheoLoai(id);

        ResultCount.Text = TotalRecord.ToString();

        Paging page = getPagingObject(TotalRecord, currentPage, PagingSize);

        PageString = page.BuildPaging();
    }

    private void LoadData()
    {
        LoaiSanPhamDataSet lspDS = new LoaiSanPhamHelper().LoadLoaiSanPham(Convert.ToInt32(Request.QueryString["id"]));
        ddlLoaiCha.Items.Add(new ListItem(lspDS.LoaiSanPham[0].TenLoaiSanPham,lspDS.LoaiSanPham[0].LoaiSanPhamID.ToString()));
        ddlLoaiCha.Enabled=false;


    }


    public string GetLoaiSanPhamName()
    {
        LoaiSanPhamHelper lspHP = new LoaiSanPhamHelper();
        LoaiSanPhamDataSet lspDs = lspHP.LoadLoaiSanPham(id);
        if (lspDs.LoaiSanPham.Rows.Count == 0)
        {
            return "Tất cả";
        }
        LoaiSanPhamDataSet.LoaiSanPhamRow lsp = lspDs.LoaiSanPham[0];
        if (lsp.IsLoaiSanPhamChaIDNull())
        {
            return lsp.TenLoaiSanPham;
        }
        LoaiSanPhamDataSet lspDsCha = lspHP.LoadLoaiSanPham(lsp.LoaiSanPhamChaID);
        LoaiSanPhamDataSet.LoaiSanPhamRow lspCha = lspDsCha.LoaiSanPham[0];

        return lspCha.TenLoaiSanPham + "  >>  " + lsp.TenLoaiSanPham;
    }

    public string GetLoaiSanPham(object loaisanphamID)
    {
        if (loaisanphamID!= DBNull.Value)
        {
            LoaiSanPhamDataSet lspDS = new LoaiSanPhamHelper().LoadLoaiSanPham(Convert.ToInt32( loaisanphamID));
            return lspDS.LoaiSanPham[0].TenLoaiSanPham;

        }
        return "";
    }

    public string GetNgayTao(object ngaytao)
    {
        if (ngaytao!=DBNull.Value)
        {
            return CommonHelper.ConvertDate(ngaytao, Constants.AdminUI);

        }
        return "";
    }

    protected void btnOK_OnClick(object sender, EventArgs e)
    {
        Page.Validate();
        if (!Page.IsValid) return;
        SanPhamHelper spHP = new SanPhamHelper();
        SanPhamDataSet spDS = new SanPhamDataSet();
        SanPhamDataSet.SanPhamRow sanphammoi = spDS.SanPham.NewSanPhamRow();
        sanphammoi.TenSanPham = txtTenSanPham.Text;
        sanphammoi.LoaiSanPham =Convert.ToInt32( ddlLoaiCha.SelectedValue);
        sanphammoi.HangSanXuat = txtHangSanXuat.Text;
        sanphammoi.ChungLoai = txtChungLoai.Text;
        sanphammoi.Gia =Convert.ToInt32( txtGia.Text);
        UploadImage();
        if (txtHinhAnh.Text == string.Empty)
            {
                txtHinhAnh.Text = "noimage.jpg";
            }
        sanphammoi.HinhAnh = txtHinhAnh.Text;
        sanphammoi.SanPhamMoi = ckSanPhamMoi.Checked;
        if (txtThuTu.Text!="")
        {
            sanphammoi.ThuTu = Convert.ToInt32(txtThuTu.Text);
        }
        sanphammoi.NgayDang = DateTime.Now;

        sanphammoi.TongQuan = txtTongQuan.Text;
        sanphammoi.ThongSoKyThuat = txtThongSo.Text;
        sanphammoi.Album = txtAlbum.Text;
        spDS.SanPham.AddSanPhamRow(sanphammoi);
        spHP.SaveSanPham(spDS);
        RedirectPage(Request.RawUrl,true);
    }

    private void UploadImage()
    {
        if (txtHinhAnh.Text != string.Empty)
        {
            //delete old image
            try
            {
                string saveLocation = MyFilePathHelper.GetImageFolderURL_Saving(this);
                MyFilePathHelper.DeleteResource(saveLocation, txtHinhAnh.Text);
            }
            catch (Exception)
            {
            }
            finally
            {
                txtHinhAnh.Text = "noimage.jpg";
            }
            //postpone new image
        }
        //save new image
        if (this.inputFile.PostedFile != null && this.inputFile.PostedFile.ContentLength > 0)
        {
            string fn = System.IO.Path.GetFileName(inputFile.PostedFile.FileName);
            string path = MyFilePathHelper.GetImageFolderURL_Saving(this);
            fn = MyFilePathHelper.AppendTimeStamp(fn);
            string saveLocation = path + fn;

            inputFile.PostedFile.SaveAs(saveLocation);
            FileStream fs = File.Open(saveLocation, FileMode.Open, FileAccess.Read);
            byte[] inByte = new byte[fs.Length];
            fs.Read(inByte, 0, inByte.Length);
            fs.Close();
            MyFilePathHelper.DeleteResource(path , fn);
            ImagesConvertHelper iconvert = new ImagesConvertHelper();
            string filename;
            if (iconvert.ConvertImage(saveLocation, inByte, 6, out filename) == false)
                txtHinhAnh.Text = string.Empty;
            else
            {
                txtHinhAnh.Text =   fn + ".JPG";
            }
        }
    }

    protected void learnMoreButton_Click(object sender, EventArgs e)
    {
        int id = Convert.ToInt32(((Button)sender).CommandArgument);
        SanPhamDataSet bannerDs = new SanPhamHelper().LoadSanPham(id);
        bannerDs.SanPham[0].Delete();
        new SanPhamHelper().SaveSanPham(bannerDs);
        RedirectPage(Request.RawUrl, true);
    }

    protected void repSanPham_OnItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Button repCon = ((Button)e.Item.FindControl("learnMoreButton"));
            repCon.Attributes.Add("onclick", "if (!confirm('Xóa dữ liệu?')) return;");
        }
    }

    protected void bttSearch_OnClick(object sender, EventArgs e)
    {
        //string tmpPage = Request.QueryString["pg"];
        int currentPage = 1;
        //if (tmpPage != null && tmpPage != "")
        //{
        //    currentPage = Convert.ToInt32(tmpPage);
        //}
        SanPhamDataSet spDs = new SanPhamHelper().LoadSanPhamTheoLoai(currentPage, PagingSize, "NgayDang", "DESC", id,txtSearch.Text);

        repSanPham.DataSource = spDs.SanPham;
        repSanPham.DataBind();

        TotalRecord = new SanPhamHelper().TongSanPhamTheoLoai(id);

        ResultCount.Text = TotalRecord.ToString();

        Paging page = getPagingObject(TotalRecord, currentPage, PagingSize);

        PageString = page.BuildPaging();
    }



}